<template lang="pug">
  div
    .main
      .change-password-wrapper
        .change-password.col-md-6.col-lg-5.col-xl-4
          h2.title.row
            .col-sm-4 &nbsp;
            .col-sm-8 {{title}}
          br
          SmartForm(
            v-model="formValues"
            :labelSize=5
            :formList="formList"
            :columns="columns"
            :buttonList="buttonList")
</template>

<script>
import { logout } from '@/utils/auth'
import { encrypt } from '@/utils/common'
import SmartForm from '@/components/SmartForm'

export default {
  name: 'change-password',
  components: {
    SmartForm
  },
  data () {
    return {
      title: '修改密码',
      buttonList: [{
        name: 'submit',
        label: '提交',
        validate: true,
        func: ({funcProps}) => {
          const formValues = funcProps.form.formValues
          this.$patch({
            url: 'dxbase/rbac/user/changePassword',
            data: {
              oldPassword: encrypt(formValues.oldPassword),
              newPassword: encrypt(formValues.newPassword)
            }
          }).then(data => {
            if (data) {
              logout(() => {
                this.$message({
                  message: '修改密码成功，请重新登录！',
                  showClose: true,
                  type: 'success'
                })
                this.$router.replace('/login')
              })
            }
          })
        }
      }],
      formValues: {
        oldPassword: '',
        newPassword: ''
      },
      formList: ['oldPassword', 'newPassword', 'repeatPassword'],
      columns: {
        oldPassword: {
          label: '旧密码',
          form: {
            type: 'password',
            rules: [
              { type: 'required', value: true, message: '请输入旧密码' }
            ]
          }
        },
        newPassword: {
          label: '新密码',
          form: {
            type: 'password',
            rules: [{
              type: 'required',
              value: true,
              message: '请输入新密码'
            }, {
              type: 'reg',
              value: /^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z]).*$/,
              message: '密码为字母加数字，最少六位'
            }]
          }
        },
        repeatPassword: {
          label: '重复输入新密码',
          form: {
            type: 'password',
            rules: [
              { type: 'required', value: true, message: '请输入新密码' },
              { type: 'sameAs', value: 'newPassword', message: '和新密码保持一致' }
            ]
          }
        }
      }
    }
  }
}
</script>

<style lang="sass" scoped>
.change-password-wrapper
  margin: 40px
.change-password
  margin: 40px auto
</style>
